﻿using LXYYY.Models;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.IdentityServer.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace LXYYY.EntityFrameworkCore
{
    [ReplaceDbContext(typeof(IIdentityDbContext))]
    [ReplaceDbContext(typeof(ITenantManagementDbContext))]
    [ConnectionStringName("Default")]
    public class LXYYYDbContext : 
        AbpDbContext<LXYYYDbContext>,
        IIdentityDbContext,
        ITenantManagementDbContext
    {
        /* Add DbSet properties for your Aggregate Roots / Entities here. */

        #region Entities from the modules

        /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
         * and replaced them for this DbContext. This allows you to perform JOIN
         * queries for the entities of these modules over the repositories easily. You
         * typically don't need that for other modules. But, if you need, you can
         * implement the DbContext interface of the needed module and use ReplaceDbContext
         * attribute just like IIdentityDbContext and ITenantManagementDbContext.
         *
         * More info: Replacing a DbContext of a module ensures that the related module
         * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
         */
        public DbSet<Collection> Collection { get; set; }
        public DbSet<OrderInfo> OrderInfo { get; set; }
        public DbSet<OrderInfomation> OrderInfomation { get; set; }
        public DbSet<ShoppingCar> ShoppingCar { get; set; }
        public DbSet<MyGoodsImg> MyGoodsImg { get; set; }
        public DbSet<MyGoodsInformation> MyGoodsInformation { get; set; }
        public DbSet<MyGoodsInventory> MyGoodsInventory { get; set; }
        public DbSet<MyGoodsSpecifications> MyGoodsSpecifications { get; set; }
        public DbSet<MyGoodsType> MyGoodsType { get; set; }
        public DbSet<MyUserAddr> MyUserAddr { get; set; }

        public DbSet<MyUserConsumption> MyUserConsumption { get; set; }
        public DbSet<MyUserInformation> MyUserInformation { get; set; }
        public DbSet<MyUserMembers> MyUserMembers { get; set; }
        public DbSet<MyUserPay> MyUserPay { get; set; }
        public DbSet<Power> Power { get; set; }
        public DbSet<Role> Role { get; set; }
        public DbSet<RolePower> RolePower { get; set; }
        public DbSet<UserInfos> UserInfos { get; set; }
        public DbSet<UserRole> UserRole { get; set; }

        //Identity
        public DbSet<IdentityUser> Users { get; set; }
        public DbSet<IdentityRole> Roles { get; set; }
        public DbSet<IdentityClaimType> ClaimTypes { get; set; }
        public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
        public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
        public DbSet<IdentityLinkUser> LinkUsers { get; set; }
        
        // Tenant Management
        public DbSet<Tenant> Tenants { get; set; }
        public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

        #endregion
        
        public LXYYYDbContext(DbContextOptions<LXYYYDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity<MyGoodsImg>(b =>
            {
                b.ToTable("MyGoodsImg");
            });

            builder.Entity<MyGoodsInformation>(b =>
            {
                b.ToTable("MyGoodsInformation");
            });

            builder.Entity<MyGoodsInventory>(b =>
            {
                b.ToTable("MyGoodsInventory");
            });
            builder.Entity<MyGoodsSpecifications>(b =>
            {
                b.ToTable("MyGoodsSpecifications");
            });
            builder.Entity<MyGoodsType>(b =>
            {
                b.ToTable("MyGoodsType");
            });
            builder.Entity<MyUserAddr>(b =>
            {
                b.ToTable("MyUserAddr");
            });
            builder.Entity<MyUserConsumption>(b =>
            {
                b.ToTable("MyUserConsumption");
            });
            builder.Entity<MyUserInformation>(b =>
            {
                b.ToTable("MyUserInformation");
            });
            builder.Entity<MyUserMembers>(b =>
            {
                b.ToTable("MyUserMembers");
            });
            builder.Entity<MyUserPay>(b =>
            {
                b.ToTable("MyUserPay");
            });
            builder.Entity<Power>(b =>
            {
                b.ToTable("Power");
            });
            builder.Entity<Role>(b =>
            {
                b.ToTable("Role");
            });
            builder.Entity<RolePower>(b =>
            {
                b.ToTable("RolePower");
            });
            builder.Entity<UserInfos>(b =>
            {
                b.ToTable("UserInfos");
            });
            builder.Entity<UserRole>(b =>
            {
                b.ToTable("UserRole");
            });

            builder.Entity<Collection>(b =>
            {
                b.ToTable("Collection");
            });
            builder.Entity<OrderInfo>(b =>
            {
                b.ToTable("OrderInfo");
            });
            builder.Entity<OrderInfomation>(b =>
            {
                b.ToTable("OrderInfomation");
            });
            builder.Entity<ShoppingCar>(b =>
            {
                b.ToTable("ShoppingCar");
            });
            /* Include modules to your migration db context */

            builder.ConfigurePermissionManagement();
            builder.ConfigureSettingManagement();
            builder.ConfigureBackgroundJobs();
            builder.ConfigureAuditLogging();
            builder.ConfigureIdentity();
            builder.ConfigureIdentityServer();
            builder.ConfigureFeatureManagement();
            builder.ConfigureTenantManagement();

            /* Configure your own tables/entities inside here */

            //builder.Entity<YourEntity>(b =>
            //{
            //    b.ToTable(LXYYYConsts.DbTablePrefix + "YourEntities", LXYYYConsts.DbSchema);
            //    b.ConfigureByConvention(); //auto configure for the base class props
            //    //...
            //});
        }
    }
}
